function addListener(a, c, b){
    if (a.addEventListener) {
        a.addEventListener(c, b, false)
    }
    else {
        a.attachEvent("on" + c, b)
    }
}

function getElementPos(el) {
    var _x = 0, _y = 0;
    do {
        _x += el.offsetLeft;
        _y += el.offsetTop;
		if(el.style.position == 'relative') break;
    } while (el = el.offsetParent);
    return { x: _x, y: _y };
}

var GoogleSuggest = function(j, o, c, dd){
    var h = {
        acClass: "active",
        sugCase: "mod_gg_sug",
        sugUl: "mod_gg_sug_list",
        sugBtn: "mod_gg_sug_btn"
    };
    var u = document.getElementById(j), a = document.getElementById(o), q = document.createElement("div"), p = document.createElement("ul"), t = document.createElement("span");
	if(null == u || null == a) {
		return ;	
	}
	var baseurl=$("#baseurl").val();
    var d = this, b = -1, k = null, g = 300, x = a.value, r = a.value, l = 0;
    var i = '';
    if(dd == 'out'){
    	i = baseurl+"index.php/outside_contract/autosearch/";
	}else{
		i = baseurl+"index.php/contacter/autosearch/";
	}
    var y = function(A){
		/*
        if (A == "") {
            q.style.display = "none";
            x = A;
            return false
        }
        else {
			*/
            if (q.style.display != "none" && A == x) {
                return false
            }
            else {
                return true
            }
				/*
        }
		*/
    };
    var n = function(A){
        return id = Number(A.replace(h.sugCase, ""))
    };
    this.IsSuggest = true;
    var f = function(){
        t.id = h.sugBtn;
        t.innerHTML = "关闭";
        p.id = h.sugUl;
        q.id = h.sugCase;
        q.appendChild(p);
        q.appendChild(t);
        u.appendChild(q);



		pos = getElementPos(a);

        q.style.left = pos.x + 'px';
        q.style.top = pos.y - 1 + 21 + 'px';
        if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE6.0") {
            q.style.width = a.offsetWidth + 'px';
        }
        else {
            q.style.width = a.offsetWidth - 2 + 'px';
        }

		/*
		u.style.position = "relative";

        q.style.left = a.offsetLeft;
        q.style.top = a.offsetHeight - 1;
        if (document.all) {
            q.style.width = a.offsetWidth
        }
        else {
            q.style.width = a.offsetWidth - 2
        }
		*/

    };

    this.FormSubmit = function(A){
        a.value = A;
        //u.submit()
    };

    this.SuggestOff = function(){
        q.style.display = "none";
        p.innerHTML = "";
        this.IsSuggest = false
    };
    this.GetSuggest = function(){
        var C = a.value;
        if (y(C)) {
            var A = i + encodeURIComponent(C);
            if (B) {
                B.parentNode.removeChild(B)
            }
            var B = document.createElement("script");
            document.body.appendChild(B);
            B.charset="utf-8";
            B.src = A
        }
    };
    var m = function(){
        window.baidu = {};
        window.baidu.sug = function(F){
            p.innerHTML = "";
            b = -1;
            x = r = F.q;
            if (F.s.length == 0) {
                q.style.display = "none";
                l = 0
            }
            else {
                var D = [], C = 0, B = F.s.length;
                for (var E = 0; E < B; E++) {
                    var A = "<li id=" + h.sugCase + C + "><p>" + F.s[E] + "</p></li>"
                    D.push(A);
                    C++
                }
                p.innerHTML = D.join("");
                q.style.display = "block";
                l = 1;
                e()
            }
        }
    };
    var e = function(){
        var B = p.getElementsByTagName("li"), A = B.length;
        for (var C = 0; C < A; C++) {

        	B[C].onclick = function(){
                d.FormSubmit(this.getElementsByTagName("p")[0].innerHTML)
				var name=this.getElementsByTagName("p")[0].innerHTML;
				if($("#type").val()==0){
				  /*销售机会获取客户联系人*/
	            	$("#contacter_name").html('');
	            	$.ajax({
					                type: "POST",
					                dataType: "text",
					                url: "index.php/opportunity/check_contacter",
					                data: "name="+name,
					                success: function(data) {
					                	data=$.parseJSON(data);
					                	var array='';
										$.each(data,function(index,nw){
											array+="<option value="+nw.id+">"+nw.name+"</option>";
					                	 });
					                	$("#contacter_name").append(array);
					                }
					            });
				}

				if($("#type").val()==1){
				   /*合同/订单获取对应机会*/
					$("#opportunity_subject").html('');
					$.ajax({
				                type: "POST",
				                dataType: "text",
				                url: "index.php/contract/check_opportunity",
				                data: "name="+name,
				                success: function(data) {
				                	 data=$.parseJSON(data);
				                	 var array='';
				                	 for(var i=0;i<data.length;i++){
				                		 $.each(data[i],function(index,nw){
												array+="<option value="+nw.id+">"+nw.subject+"</option>";
							             });
				                	 }
				                	 $("#opportunity_subject").append(array);
				                }
				            });
				}

				if($("#type").val()==2){ /*客户联系人获取合同*/
					var type_record = $("#type_record").val();
	            	$("#plan_contacter_name").html('');
	            	$.ajax({
				                type: "POST",
				                dataType: "text",
				                url: "index.php/contract/get_contract",
				                data: "name="+name+"&type_record="+type_record+"",
				                success: function(data) {
				                	 data=$.parseJSON(data);
				                	 var array='';
				                	 $.each(data,function(index,nw){
											array+="<option value="+nw.contract_id+">"+nw.subject+"</option>";
				                	 });
				                	 $("#plan_contacter_name").append(array);
				                	 if($("#plan_contacter_name").val()){ /*填充期次*/
				                		var plan_id = $("#plan_contacter_name").val();
				             			if(!plan_id){
				             				alert('请先选择合同/订单');
				             			}else{
				             				$.ajax({
				             	                type: "POST",
				             	                dataType: "text",
				             	                url: "index.php/payment_record/get_plan_stages",
				             	                data: "plan_id="+plan_id,
				             	                success: function(data) {
				             	                	var array='';
				             	                	data=$.parseJSON(data);
				             	                	var nums = data['nums']; //实际期次
				             	                	var stages = data['stages']; //总期次
				             	                	if(!type_record){
				             	                		
				             	                	}else{
					             	                		if(nums == stages){
						             	                		for(var i=1;i<=nums;i++){
						             		                		array+="<option>"+i+"</option>";
						             			                }
						             			             }else{
						             			            	 var n=1;
						             			            	 for(var i=0;i<nums;i++){ 
						             			            		array+="<option>"+((stages-nums)+n)+"</option>";
						             			            		n++;
						             				             }
						             				         }
				             	                	}
				             	                	$("#record_stages").append(array);	
				             	                }
				             	            });
				             			}
				                	 }
				                }
				            });
				}
            };

            B[C].onmouseover = function(){
                if (b != -1) {
                    document.getElementById(h.sugCase + b).className = ""
                }
                this.className = h.acClass;
                b = n(this.id)
            }
        }
    };
    var w = function(A){
        a.value = x = A ? A.getElementsByTagName("p")[0].innerHTML : r
    };
    var z = function(C, D, B){
        if (b == -1) {
            C[D].className = h.acClass;
            b = n(C[D].id);
            w(C[D])
        }
        else {
            C[b].className = "";
            var A = B == "next" ? C[b].nextSibling : C[b].previousSibling;
            if (A) {
                A.className = h.acClass;
                b = n(A.id);
                w(A)
            }
            else {
                b = -1;
                w()
            }
        }
    };
    var s = function(C){
        if (d.IsSuggest) {
            if (l == 1) {
                var B = p.getElementsByTagName("li"), A = B.length;
                if (C == "up") {
                    z(B, A - 1, "pre")
                }
                else {
                    if (C == "down") {
                        z(B, 0, "next")
                    }
                }
            }
        }
    };
    var v = function(){
        m();
        f();
        t.onclick = function(){
            d.SuggestOff()
        };
        addListener(a, "keydown", function(A){
            var C = window.event ? window.event : A;
            var B = C.keyCode;
            if (B == 38) {
                s("up")
            }
            else {
                if (B == 40) {
                    s("down")
                }
            }
        });

        a.onfocus = function(){
            if (d.IsSuggest) {
                if (k) {
                    clearInterval(k)
                }
                k = setInterval(d.GetSuggest, g)
            }
        };
        a.onblur = function(){
            clearInterval(k)
        };
        addListener(document, "click", function(A){
            var C = window.event ? window.event : A;
            var B = A.srcElement || A.target;
            if (B.id == h.sugCase) {
                return
            }
            else {
                l = 0;
                q.style.display = "none"
            }
        })
    };
    v()
};
